package org.uam.common;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * 基础Action支撑类
 * <功能详细描述>
 * 
 * @author  Leo
 * @version  [版本号, 2012-11-3]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class BaseActionSupport extends ActionSupport implements SessionAware, ServletRequestAware,
    ServletResponseAware, ApplicationAware, ParameterAware
{
    
    /**
    * 序列号
    */
    private static final long serialVersionUID = -3808779426004781489L;
    
    private Map<String, Object> session;
    
    private HttpServletRequest request;
    
    private HttpServletResponse response;
    
    private Map<String, Object> application;
    
    private Map<String, String[]> parameters;
    
    protected Map<String, String> cookies;
    
    protected static final String SUCCESS = "success";
    
    public BaseActionSupport()
    {
        this.request = null;
        this.response = null;
        this.session = null;
        this.application = null;
        this.parameters = null;
    }
    
    public void setServletRequest(HttpServletRequest httpServletRequest)
    {
        this.request = httpServletRequest;
        
        // member.setCookieUser(getCookieValue("user"));
    }
    
    public HttpServletRequest getRequest()
    {
        return this.request;
    }
    
    public void setSession(Map<String, Object> session_temp)
    {
        this.session = session_temp;
    }
    
    public Map<String, Object> getSession()
    {
        return this.session;
    }
    
    public HttpServletResponse getResponse()
    {
        return this.response;
    }
    
    public void downloadFile(String filename)
    {
        
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        
        String tempPath = request.getSession().getServletContext().getRealPath("") + "/download/";
        
        // 设置为下载application/x-download
        response.setContentType("application/x-download");
        // 即将下载的文件在服务器上的绝对路径
        String filenamedownload = tempPath + "/" + filename;
        // // 下载文件时显示的文件保存名称
        // String filenamedisplay = name;
        // // 中文编码转换
        // filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);
        try
        {
            java.io.OutputStream os = response.getOutputStream();
            java.io.FileInputStream fis = new java.io.FileInputStream(filenamedownload);
            byte[] b = new byte[1024];
            int i = 0;
            while ((i = fis.read(b)) > 0)
            {
                os.write(b, 0, i);
            }
            fis.close();
            os.flush();
            os.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
    }
    
    public void setServletResponse(HttpServletResponse httpServletResponse)
    {
        this.response = httpServletResponse;
    }
    
    public Map<String, Object> getApplication()
    {
        return this.application;
    }
    
    public void setApplication(Map<String, Object> application_temp)
    {
        this.application = application_temp;
    }
    
    public static ServletContext getServletContext()
    {
        return ServletActionContext.getServletContext();
    }
    
    protected String getCookieValue(String name)
    {
        Cookie cookies[] = request.getCookies();
        if (cookies != null)
        {
            for (Cookie cookie : cookies)
            {
                
                if (!cookie.getName().equals(name))
                    continue;
                return cookie.getValue();
            }
            
        }
        return null;
    }
    
    protected void saveCookie(String name, String value, int maxAge)
    {
        javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie(name, value);
        cookie.setMaxAge(maxAge);
        getResponse().addCookie(cookie);
    }
    
    public Map<String, String[]> getParameters()
    {
        return this.parameters;
    }
    
    public void setParameters(Map<String, String[]> parameters_temp)
    {
        this.parameters = parameters_temp;
    }
    
    protected void putOperationName(String operationName)
    {
        this.getRequest().setAttribute(ResourceCommon.OPERATION_NAME, operationName);
    }
    
    protected void putOperationResult(String resultCode)
    {
        this.getRequest().setAttribute(ResourceCommon.OPERATION_RESULT_CODE, resultCode);
    }
}
